package com.qf.exhandler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

//统一处理异常：注解方法解决
@ControllerAdvice
public class MyExceptionhander2 {
    @ExceptionHandler({
            RuntimeException.class
    })
    public ModelAndView handlerRuntimeException(){
        return null;
    }
    @ExceptionHandler({
            ServiceException.class
    })
    public ModelAndView handlerServiceException(ServiceException e){
        e.printStackTrace();
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg",e.getMessage());
        mav.setViewName("/error.jsp");
        return mav;
    }
    @ExceptionHandler({Exception.class})
    public ModelAndView handlerOtherException(Exception e){
        e.printStackTrace();
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","系统异常，请联系管理员");
        mav.setViewName("/error.jsp");
        return mav;
    }
}
